#include "SCheckComboBox.h"
#include <QHBoxLayout>
#include <QLineEdit>

namespace qb
{
	SCheckComboBox::SCheckComboBox(QStringList aszlist, QWidget* parent)
		: QWidget(parent)
	{
		m_Combo = new QComboBox(this);
		m_Combo->setStyleSheet("QComboBox{\
			border: 1px solid rgb(50, 88, 80);\
			background: rgb(18, 26, 25);\
			color:rgb(255, 235, 200);\
			selection - background - color:rgb(19, 62, 56);\
		}");

		QHBoxLayout* mainLayout = new QHBoxLayout(this);
		mainLayout->addWidget(m_Combo);
		setLayout(mainLayout);

		m_List = new QListWidget(this);
		m_List->setStyleSheet("QListWidget{ border:1px solid gray; background: rgb(18, 26, 25); }"
			"QListWidget::Item{ padding-top:1px; padding-bottom:1px; }"
			"QListWidget::Item:hover{ background:rgb(19, 62, 56); }"
			"QListWidget::item:selected{ background:rgb(19, 62, 56); color:black; }"
			"QListWidget::item:selected:!active{ border-width:0px; background:rgb(19, 62, 56); }"
		);

		for (int i = 0; i < aszlist.size(); i++)
		{
			QListWidgetItem* item = new QListWidgetItem();
			//item->setCheckState(Qt::Unchecked);
			//item->setText(aszlist.at(i));

			m_List->addItem(item);
			QCheckBox* box = new QCheckBox();
			box->setStyleSheet("QCheckBox::indicator:unchecked{\
					padding-left:4px;\
					image:url(:/widgets/image/check/check_normal.png);\
				}\
				QCheckBox::indicator:unchecked:hover{\
					padding-left:4px;\
					image:url(:/widgets/image/check/check_hover.png);\
				}\
				QCheckBox::indicator:unchecked:pressed{\
					padding-left:4px;\
					image:url(:/widgets/image/check/check_pressed.png);\
				}\
				QCheckBox::indicator:checked{\
					padding-left:4px;\
					image:url(:/widgets/image/check_selected/check_selected_normal.png);\
				}\
				QCheckBox::indicator:checked:hover{\
					padding-left:4px;\
					image:url(:/widgets/image/check_selected/check_selected_hover.png);\
				}\
				QCheckBox::indicator:checked:pressed{\
					padding-left:4px;\
					image:url(:/widgets/image/check_selected/check_selected_pressed.png);\
				}\
			");

			box->setText(aszlist.at(i));
			m_List->setItemWidget(item, box);
			connect(box, &QCheckBox::stateChanged, this, &SCheckComboBox::UpdateComboContent);
		}

		m_Combo->setModel(m_List->model());
		m_Combo->setView(m_List);
		m_Combo->setEditable(true);
		QLineEdit* lineEdit = m_Combo->lineEdit();
		if (lineEdit)
		{
			lineEdit->setReadOnly(true);
		}
	}

	SCheckComboBox::~SCheckComboBox()
	{

	}

	void SCheckComboBox::UpdateComboContent()
	{
		QString str;
		for (int i = 0; i < m_List->count(); i++)
		{
			QListWidgetItem* item = m_List->item(i);
			QCheckBox* box = (QCheckBox*)m_List->itemWidget(item);
			if (box->checkState() == Qt::Checked)
			{
				str += box->text();
				str += ";";
			}
		}
		str = str.mid(0, str.size() - 1);
		m_Combo->lineEdit()->setText(str);
	}

	void SCheckComboBox::SetItemCheckStates(int nState)
	{
		for (int i = 0; i < m_List->count(); i++)
		{
			if (nState % 2 == 1)
			{
				QListWidgetItem* item = m_List->item(i);
				QCheckBox* box = (QCheckBox*)m_List->itemWidget(item);
				box->setChecked(true);
			}
			nState /= 2;
		}
		UpdateComboContent();
	}

	std::vector<int> SCheckComboBox::GetItemCheckStates() const
	{
		std::vector<int> vctCheck;

		int nState = 0;
		for (int i = 0; i < m_List->count(); i++)
		{
			QListWidgetItem* item = m_List->item(i);
			QCheckBox* box = (QCheckBox*)m_List->itemWidget(item);
			vctCheck.push_back(int(box->isChecked()));
		}
		return vctCheck;
	}

	int SCheckComboBox::GetItemCount() const
	{
		return m_List->count();
	}

	QCheckBox* SCheckComboBox::GetCheckBoxFromIndex(int nIndex) const
	{
		if ((nIndex < 0) || (nIndex > m_List->count() - 1))
			return NULL;
		QListWidgetItem* item = m_List->item(nIndex);
		QCheckBox* box = (QCheckBox*)m_List->itemWidget(item);
		return box;
	}
}